$(init);

function init () {
	$("div.left").height(($(window.document).height()) - 85);
	
	// 初始化法规类别树
	initFgTypeTree();
	
	// 初始化法规表格
	initFgGrid();
}

function initFgTypeTree () {
	var zNodes = doService(ctx + "/zglj/getFgTypeTree");
	console.log(zNodes);
	var setting = {
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: null
			}
		},
		callback: {
			onClick: fgTypeClick
		}
	};
	$.fn.zTree.init($("#fgTypeTree"), setting, zNodes);
}

function initFgGrid () {
	$("#fgxxGrid").jqGrid({
        url: ctx + '/common/extQuery?sqlid=zcfg',
        autowidth: true,
        height: (($(window.document).height()) - 160),
        datatype: 'json',
        postData : $("#searchForm").getFormJson(),
        colNames: ['标题', '文号', '类别', '发布日期', '操作', 'fgid', 'type_id'],
        colModel: [
            {name: 'title', index: 'title', width: 250, sortable: false, formatter: function (value, options, row) {
            	return "<a href=\"javascript:detailFg('" + row.fgid + "','" + row.type_id + "', '" + row.title + "')\">" + value + "</a>";
            }},
            {name: 'wh', index: 'wh', width: 80, sortable: false},
            {name: 'type_name', index: 'type_name', width: 50, sortable: false},
            {name: 'fbrq', index: 'fbrq', width: 50, sortable: false},
            {name: 'cz', index: 'cz', width: 50, sortable: false, align: "center", title: false, formatter: function (value, options, row) {
                    return "<span class='tdcz'>" +
                            "<a title='查看' class='small_btn look' href=javascript:detailFg('" + row.fgid + "','" + row.type_id + "');>详情</a>" +
                            "</span>";
                }},
            {name: 'fgid', index: 'fgid', hidden: true},
            {name: 'type_id', index: 'type_id', hidden: true}
        ],
        prmNames: {
            rows: "limit"
        },
        jsonReader: {
            root: "message.list",
            page: "message.pageNumber",
            total: "message.totalPage",
            records: "message.totalRow"
        },
        loadtext: "<img src='" + ctx + "/static/images/loading.gif'>&nbsp;&nbsp;拼命加载中...",
        rowNum: 50,
        rowList: [50, 100, 150],
        pager: '#fgxxPager',
        mtype: "POST",
        viewrecords: true,
        sortable: false
    });
}

/**
 * 刷新表格
 * @returns
 */
function refreshFGXX () {
	formdata = $("#searchForm").getFormJson();
    $("#fgxxGrid").setGridParam({"postData": formdata}).trigger("reloadGrid",[{page: 1}]);
}

/**
 * 重置搜索条件form
 * @returns
 */
function resetForm () {
	$("#name").val('');
}


/**
 * 左侧类别点击事件
 * @param event
 * @param treeId
 * @param treeNode
 * @returns
 */
function fgTypeClick (event, treeId, treeNode) {
	if(treeNode.pId) {
		$("#type_id").val(treeNode.id);
		refreshFGXX();
	}
}

/**
 * 显示详情页面
 * @param fgid
 * @param type_id
 * @param title
 * @returns
 */
function detailFg(fgid, type_id) {
	var title = '政策法规详情';
	parent.openTab("infoDetail"+fgid,title,ctx+"/widget/zcfgDetailIndex?id="+fgid+"&type_id="+type_id);
}